﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：CartInfo.cs
//
//
//文件功能描述：顾客结帐时要填写的信息
//
//
//创建标识：2007.11.21 翟丛兴
//创建描述：顾客填写送货信息
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 顾客结帐时要填写的送货信息
/// </summary>
public partial class Common_Controls_Balance : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //保存购物车中商品列表
            DataSet dsCartList = new DataSet();
            //Session["CustomerID"] = "CUS-00000000";

            //得到省份下拉列表
            GetProviceAndCity();

            //判断顾客是否已填写了送货信息
            if (Session["OrderInfoModel"] != null)
            {
                //如果顾客已填写了送货信息，从Session中取出填写的信息，显示到桌面上
                //得到保存在Session中的订单信息实体的对象，并将其信息显示到页面上
                localhost.OrderInfo orderInfoModelObj = (localhost.OrderInfo)Session["OrderInfoModel"];
                txtBuyerFirstName.Text = orderInfoModelObj.BillToFirstName;
                txtBuyerLastName.Text = orderInfoModelObj.BillToLastName;
                txtSenderFirstName.Text = orderInfoModelObj.ShipToFirstName;
                txtSenderLastName.Text = orderInfoModelObj.ShipToLastName;

                //如果顾客注册时填写的省份信息是标准信息如“江西省”就将ddlPorvice中对应的项设为选中，如果输入的不是标准信息，
                //将当前选中项，改为注册时填写的信息，虽然这样有缺陷，但在没有更好的解决办法时，先这样更好
                if (ddlProvince.Items.FindByText(orderInfoModelObj.ShipProvince.Trim()) != null)
                {
                    ddlProvince.Items.FindByText(orderInfoModelObj.ShipProvince.Trim()).Selected = true;
                }
                else
                {
                    ddlProvince.SelectedItem.Text = orderInfoModelObj.ShipProvince.Trim();
                }
                //同上省份信息的处理
                if (ddlCity.Items.FindByText(orderInfoModelObj.ShipCity.Trim()) != null)
                {
                    ddlProvince.Items.FindByText(orderInfoModelObj.ShipCity.Trim()).Selected = true;
                }
                else
                {
                    ddlCity.SelectedItem.Text = orderInfoModelObj.ShipCity.Trim();
                }

                txtAddress.Text = orderInfoModelObj.ShipAddress;
                txtShipZip.Text = orderInfoModelObj.ShipZip;
                txtPhone.Text = orderInfoModelObj.Phone;
                if (Session["ShipEmail"] != null)
                    txtEmail.Text = Session["ShipEmail"].ToString();
                if (orderInfoModelObj.ShipMethod == "普通平邮")
                {
                    rbtnCommanpost.Checked = true;
                    rbtnQucikPost.Checked = false;
                }
                else
                {
                    rbtnCommanpost.Checked = false;
                    rbtnQucikPost.Checked = true;
                }
                ddlBillWay.Text = orderInfoModelObj.BillMethod;
            }
            else
            {
                //如果顾客还没有填写送货信息
                //判断时注册顾客还是匿名顾客
                if ((Session["CustomerID"] != null) && (Session["CustomerID"].ToString() != String.Empty) && (Session["CustomerID"].ToString() != "CUS-00000000"))
                {
                    //如果是注册顾客
                    dsCartList = (new localhost.CartService()).GetProductList(Session["CustomerID"].ToString());

                    //判断购物车中是否有商品
                    if ((dsCartList != null) && (dsCartList.Tables[0].Rows.Count > 0))
                    {;
                        //注册顾客购物车中有商品，将注册顾客信息填入送货人信息中
                        GetCustomerInfoToSenderInfo();

                    }
                    else
                    {
                        //注册顾客购物车中没有商品
                        Response.Write("<script language=javascript> alert('你的购物这中没有商品！');window.location.href='ManageCart.aspx'</script>");
                    }
                }
                else
                {   //如果是匿名顾客，判断其购物车中是否有商品
                    if (Session["CartList"] == null)
                    {
                        //匿名顾客购物车中没有商品
                        Response.Write("<script language=javascript> alert('你的购物这中没有商品！');window.location.href='ManageCart.aspx'</script>");
                    }
                }
            }
        }
    }

    /// <summary>
    /// 确认填写的订单信息完毕，转到订单预览页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnConfirm_Click(object sender, EventArgs e)
    {

        //创建订单信息实体的对象，保存订单信息
        localhost.OrderInfo orderInfoModelObj = new localhost.OrderInfo();



        //如果已存在，丛Session中取得订单信息实体
        if (Session["OrderInfoModel"] != null)
        {
            orderInfoModelObj = (localhost.OrderInfo)Session["OrderInfoModel"];
        }

        //判断是注册顾客还是匿名顾客
        if ((Session["CustomerID"] != null) && (Session["CustomerID"].ToString() != String.Empty) && (Session["CustomerID"].ToString() != "CUS-00000000"))
        {
            //如果是注册顾客,保存其顾客ID到订单中
            orderInfoModelObj.CustomerID = Session["CustomerID"].ToString();
        }
        else
        {
            //如果是匿名顾客，采用统一的默认顾客ID"CUS-00000000"
            Session["CustomerID"] = "CUS-00000000";
            orderInfoModelObj.CustomerID = "CUS-00000000";
        }
        //添加订单信息实体的送货信息
        orderInfoModelObj.BillToFirstName = txtBuyerFirstName.Text.Trim();
        orderInfoModelObj.BillToLastName = txtBuyerLastName.Text.Trim();
        orderInfoModelObj.ShipToFirstName = txtSenderFirstName.Text.Trim();
        orderInfoModelObj.ShipToLastName = txtSenderLastName.Text.Trim();
        orderInfoModelObj.ShipProvince = ddlProvince.SelectedItem.Text.Trim();
        orderInfoModelObj.ShipCity = ddlCity.SelectedItem.Text.Trim();
        orderInfoModelObj.ShipAddress = txtAddress.Text.Trim();
        orderInfoModelObj.ShipZip = txtShipZip.Text.Trim();
        orderInfoModelObj.Phone = txtPhone.Text.Trim();
        orderInfoModelObj.BillMethod = ddlBillWay.Text.Trim();
        orderInfoModelObj.CreationTime = DateTime.Now;
        //判断送货方式
        if (rbtnCommanpost.Checked)
        {
            orderInfoModelObj.ShipMethod = rbtnCommanpost.Text.Trim();
        }
        else
        {
            orderInfoModelObj.ShipMethod = rbtnQucikPost.Text.Trim();
        }
        //保存送货人的邮件，因为数据库中没有此字段，因此只能保存到Session中
        Session["ShipEmail"] = txtEmail.Text.Trim();
        //保存订单信息实体到Session中
        Session["OrderInfoModel"] = orderInfoModelObj;

        //转到订单预览页面
        Response.Redirect("PreviewOrder.aspx");
    }

    /// <summary>
    /// 返回购物车管理页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGoCart_Click(object sender, EventArgs e)
    {
        Response.Redirect("ManageCart.aspx");
    }

    /// <summary>
    /// “购买人与送货人为同一人”单选按钮状态改变时
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void chkIsSame_CheckedChanged(object sender, EventArgs e)
    {
        //判断购买人与送货人为同一人是否为同一人
        if (chkIsSame.Checked == false)
        {
            //如果不是同一人，将送货人信息清空
            txtSenderFirstName.Text = String.Empty;
            txtSenderLastName.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtEmail.Text = String.Empty;
            txtPhone.Text = String.Empty;
            txtShipZip.Text = String.Empty;
        }
        else
        {
            //如果是同一人
            //判断时注册顾客还是匿名顾客
            if ((Session["CustomerID"] != null) && (Session["CustomerID"].ToString() != String.Empty) && (Session["CustomerID"].ToString() != "CUS-00000000"))
             {
                 //如果是注册顾客,将其信息作为送货人信息填入
                GetCustomerInfoToSenderInfo();
             }
             else
             {
                 //如果是匿名顾客，将其送货人姓名设为购买人姓名
                 if(txtBuyerFirstName.Text != String.Empty) 
                     txtSenderFirstName.Text = txtBuyerFirstName.Text;
                 if(txtBuyerLastName.Text != String.Empty)
                     txtSenderLastName.Text  = txtBuyerLastName.Text;
             }
        }
    }

    /// <summary>
    /// 将注册用户的信息添加到送货信息的输入框中
    /// </summary>
    protected void GetCustomerInfoToSenderInfo()
    {
        //异步调用WebSerive取得注册顾客的信息，取得完成后回调回调将注册顾客信息填入到送货信息
        localhost.CustomerService customerServiceObj = new localhost.CustomerService();
        customerServiceObj.GetCustomerDetailInfoCompleted += new localhost.GetCustomerDetailInfoCompletedEventHandler(customerServiceObj_GetCustomerDetailInfoCompleted);
        customerServiceObj.GetCustomerDetailInfoAsync(Session["CustomerID"].ToString());
    }

    /// <summary>
    /// 将注册顾客的信息添加到送货人信息中的异步回调函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void customerServiceObj_GetCustomerDetailInfoCompleted(Object sender, localhost.GetCustomerDetailInfoCompletedEventArgs e)
    {
        try
        {
            //得到异步调用的结果，即顾客的信息
            localhost.Customer customerModelObj = (localhost.Customer)e.Result;

            //显示顾客信息到页面上
            txtBuyerFirstName.Text = customerModelObj.FirstName;
            txtBuyerLastName.Text = customerModelObj.LastName;
            txtSenderFirstName.Text = customerModelObj.FirstName;
            txtSenderLastName.Text = customerModelObj.LastName;

            //如果顾客注册时填写的省份信息是标准信息如“江西省”就将ddlPorvice中对应的项设为选中，如果输入的不是标准信息，
            //将当前选中项，改为注册时填写的信息，虽然这样有缺陷，但在没有更好的解决办法时，先这样更好
            if (ddlProvince.Items.FindByText(customerModelObj.Provice.Trim()) != null)
            {
                ddlProvince.Items.FindByText(customerModelObj.Provice.Trim()).Selected = true;
            }
            else
            {
                ddlProvince.SelectedItem.Text = customerModelObj.Provice;
            }
            //同上省份信息的处理
            if (ddlCity.Items.FindByText(customerModelObj.City.Trim()) != null)
            {
                ddlProvince.Items.FindByText(customerModelObj.Provice.Trim()).Selected = true;
            }
            else
            {
                ddlCity.SelectedItem.Text = customerModelObj.City;
            }
            txtAddress.Text = customerModelObj.Address;
            txtEmail.Text = customerModelObj.Email;
            txtPhone.Text = customerModelObj.Phone;
            txtShipZip.Text = customerModelObj.Zip;
        }
        catch
        {
           //此时如果发生异常，不影响使用，所以只需屏蔽此异常
        }
    }

    /// <summary>
    /// 从XML文件中得到省份列表和城市列表
    /// </summary>
    protected void GetProviceAndCity()
    {
        try
        {
            DataSet dsProviceList = new DataSet();
            dsProviceList.ReadXml(Server.MapPath("XML//CartInfo//ProvinceList.xml"));
            ddlProvince.DataSource = dsProviceList.Tables[0];
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();

            DataSet dsCityList = new DataSet();
            dsCityList.ReadXml(Server.MapPath("XML//CartInfo//CityList.xml"));
            ddlCity.DataSource = dsCityList.Tables[0];
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
        }
        catch
        {
            //此时如果发生异常，不影响使用，所以只需屏蔽此异常
        }
    }

    /// <summary>
    /// 当选择的省份变化时，相应的市随之变化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            //丛数据库读取城市列表
            DataSet dsCityList = new DataSet();
            dsCityList.ReadXml(Server.MapPath("XML//CartInfo//CityList.xml"));

            //取得相应省份的城市列表，并显示
            DataView dvCityList = new DataView();
            dvCityList = dsCityList.Tables[0].DefaultView;
            ddlCity.DataSource = dvCityList;
            dvCityList.RowFilter = "ProvinceID = " + ddlProvince.SelectedValue;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
        }
        catch
        {
            //此时如果发生异常，不影响使用，所以只需屏蔽此异常
        }
    }
}
